package iceportal1
import static groovyx.net.http.ContentType.JSON
import java.util.ArrayList

class OfficeController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }
    
    def main = {
      session.officeid = params.id
      session.officepath = "office"
    }

    def list = {
        ArrayList officeList = new ArrayList ();
      try {          
          withRest(uri: "http://localhost:8080/iceapi/services/") {
              def response = get(path:"organization/"+session.orgid+"/offices",
                              headers:['Authorization':session.authKey],
                              requestContentType: JSON, 
                              contentType: JSON)
               
               response.data.offices.entries.each {
                 Office office = new Office ()
                 office.name = it.name
                 officeList.add (office)                  
               }           
               
          }          
        } catch (e) {
        }
        [officeInstanceList: officeList, officeInstanceTotal: officeList.size()]
    }

    def create = {
    }

    def save = {
        def officeInstance = new Office(params)
      try {          
          withRest(uri: "http://localhost:8080/iceapi/services/") {
              post(path:"office",
                  headers:['Authorization':session.authKey],
                  requestContentType: JSON, 
                  contentType: JSON,
                  body: ['name': ['$': params.name], 'organizationName': ['$': session.orgid], 'prefix': ['$': params.prefix]])
          }          
          redirect(action: "list")
        } catch (e) {
          e.printStackTrace();
          officeInstance.errors.rejectValue ("name","Create failed") 
          render(view: "create", model: [officeInstance: officeInstance])
        }
    }

    def show = {
        def officeInstance = new Office (params)
      try {          
          withRest(uri: "http://localhost:8080/iceapi/services/") {
              def response = get(path:"office/"+params.id,
                              headers:['Authorization':session.authKey],
                              requestContentType: JSON, 
                              contentType: JSON)
               
               officeInstance.name = response.data.name  
               officeInstance.prefix = response.data.prefix.toInteger()
               
          }          
        } catch (e) {
        }
        if (!officeInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'office.label', default: 'Office'), params.id])}"
            redirect(action: "list")
        }
        else {
            [officeInstance: officeInstance]
        }
    }

    def edit = {
        def officeInstance = new Office (params)
      try {          
          withRest(uri: "http://localhost:8080/iceapi/services/") {
              def response = get(path:"office/"+params.id,
                              headers:['Authorization':session.authKey],
                              requestContentType: JSON, 
                              contentType: JSON)
               
               officeInstance.name = response.data.name  
               officeInstance.prefix = response.data.prefix.toInteger()
               
          }          
        } catch (e) {
        }
        if (!officeInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'office.label', default: 'Office'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [officeInstance: officeInstance]
        }
    }

    def update = {
        def officeInstance = Office.get(params.id)
        if (officeInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (officeInstance.version > version) {
                    
                    officeInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'office.label', default: 'Office')] as Object[], "Another user has updated this Office while you were editing")
                    render(view: "edit", model: [officeInstance: officeInstance])
                    return
                }
            }
            officeInstance.properties = params
            if (!officeInstance.hasErrors() && officeInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'office.label', default: 'Office'), officeInstance.id])}"
                redirect(action: "show", id: officeInstance.id)
            }
            else {
                render(view: "edit", model: [officeInstance: officeInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'office.label', default: 'Office'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def officeInstance = Office.get(params.id)
        if (officeInstance) {
            try {
                officeInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'office.label', default: 'Office'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'office.label', default: 'Office'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'office.label', default: 'Office'), params.id])}"
            redirect(action: "list")
        }
    }
}
